package com.wushuu.ce.utils;

import android.util.Log;

import com.wushuu.ce.model.Expression;
import com.wushuu.ce.model.RuleModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by js on 2016/5/11.
 */
public class Rule {

    private static Rule instance;
    private Map<String, RuleModel> ruleMap;
    private static final int TEST_NUM = 20;

    private Rule(){
        ruleMap = new HashMap<String, RuleModel>();
        ruleMap.put("1", new RuleModel(1,1,0,10));
        ruleMap.put("2", new RuleModel(2,2,0,10));
        ruleMap.put("3", new RuleModel(1,2,0,10));
        ruleMap.put("4", new RuleModel(3,3,0,10));
        ruleMap.put("5", new RuleModel(4,4,0,10));
        ruleMap.put("6", new RuleModel(3,4,0,10));
        ruleMap.put("7", new RuleModel(5,5,0,10));
        ruleMap.put("8", new RuleModel(1,5,0,10));
        ruleMap.put("9", new RuleModel(6,6,0,15));
        ruleMap.put("10", new RuleModel(7,7,0,15));
        ruleMap.put("11", new RuleModel(6,7,0,15));
        ruleMap.put("12", new RuleModel(8,8,0,15));
        ruleMap.put("13", new RuleModel(9,9,0,15));
        ruleMap.put("14", new RuleModel(8,9,0,15));
        ruleMap.put("15", new RuleModel(6,9,0,15));
        ruleMap.put("16", new RuleModel(1,9,0,15));
        ruleMap.put("17", new RuleModel(1,1,10,20));
        ruleMap.put("18", new RuleModel(2,2,10,20));
        ruleMap.put("19", new RuleModel(3,3,10,20));
        ruleMap.put("20", new RuleModel(4,4,10,20));
        ruleMap.put("21", new RuleModel(5,5,10,20));
        ruleMap.put("22", new RuleModel(6,6,10,20));
        ruleMap.put("23", new RuleModel(7,7,10,20));
        ruleMap.put("24", new RuleModel(8,8,10,20));
        ruleMap.put("25", new RuleModel(9,9,10,20));
        ruleMap.put("26", new RuleModel(10,10,10,20));
        ruleMap.put("27", new RuleModel(1,5,10,20));
        ruleMap.put("28", new RuleModel(6,10,10,20));
        ruleMap.put("29", new RuleModel(1,10,10,20));
        ruleMap.put("30", new RuleModel(10,10,10,30));
        ruleMap.put("31", new RuleModel(11,11,10,30));
        ruleMap.put("32", new RuleModel(12,12,10,30));
        ruleMap.put("33", new RuleModel(13,13,10,30));
        ruleMap.put("34", new RuleModel(14,14,10,30));
        ruleMap.put("35", new RuleModel(15,15,10,30));
        ruleMap.put("36", new RuleModel(1,10,0,30));
        ruleMap.put("37", new RuleModel(11,20,0,30));
        ruleMap.put("38", new RuleModel(21,30,0,30));
        ruleMap.put("39", new RuleModel(1,30,0,30));
        ruleMap.put("40", new RuleModel(1,30,0,30));
    }
    public static Rule getInstance(){
        if(instance == null){
            instance = new Rule();
        }
        return instance;
    }

    public List<Expression> getPracticeList(String chapter){
        List<Expression> practiceList = new ArrayList<Expression>();
        RuleModel rule = null;
        for(Map.Entry<String, RuleModel> entry : ruleMap.entrySet()){
            if(entry.getKey().equals(chapter)){
                rule = entry.getValue();
            }
        }
        if (rule == null)
            return null;
        Random random = new Random();
        Expression expression ;
        int limit = (int) Math.ceil(TEST_NUM / (rule.getEnd() - rule.getBegin() + 1));
        System.out.println("limit------------->"+limit);
        if(limit == 0){
            for(int i = 0; i < TEST_NUM; i++){
                int randNum1 = random.nextInt(rule.getMax()-rule.getMin());
                int randNum2 = random.nextInt(rule.getMax()-randNum1);
                expression = new Expression(String.valueOf(randNum1)+"+"+ String.valueOf(randNum2)+"=",randNum1+randNum2);
                practiceList.add(expression);
            }
        }else{
            for(int i = rule.getBegin(); i <= rule.getEnd(); i++){
                if(rule.getBegin() == rule.getEnd()){
                    int min = (rule.getMin()-rule.getBegin())<1?1:(rule.getMin()-rule.getBegin());
                    for(int j = min; j <= rule.getMax()-i; j++){
                        expression = new Expression(String.valueOf(i)+"+"+ String.valueOf(j)+"=",i+j);
                        practiceList.add(expression);
                    }
                    for(int j = rule.getMax()-i; j < TEST_NUM+min-1; j++){
                        int randNum = random.nextInt(rule.getMax()-rule.getBegin())+1;
                        expression = new Expression(String.valueOf(i)+"+"+ String.valueOf(randNum)+"=",i+randNum);
                        practiceList.add(expression);
                    }
                }else{
                    for (int j = 1; j <=limit; j++){
                        int randNum = random.nextInt(rule.getMax()-rule.getMin())+(rule.getMin()-i);
                        expression = new Expression(String.valueOf(i)+"+"+ String.valueOf(randNum)+"=",i+randNum);
                        practiceList.add(expression);
                    }
                }

            }
        }

        return practiceList;
    }

}
